<template>
  <!-- close:点击关闭会执行，且在修改visible的值为false后也会执行 -->
  <el-dialog
    title="新增员工"
    :visible="value"
    width="700px"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机号">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <!-- el-date-picker
                         v-model:值的双向绑定
                         type:类型，默认是年月日选择(date)
         -->
        <el-date-picker
          v-model="form.timeOfEntry"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <!-- readonly:只读 -->
        <div v-clickOut="outFn">
          <el-input
            v-model="form.departmentName"
            placeholder="请输入部门"
            readonly
            @focus="focusEvent"
          />
          <el-collapse-transition>
            <el-tree
              v-if="showTree"
              :data="treeData"
              :props="{ label: 'name', children: 'children' }"
              @node-click="nodeClick"
            />
          </el-collapse-transition>
        </div>
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker
          v-model="form.correctionTime"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div style="text-align: center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import employeesData from '@/api/constant/employees'
import { companyDepartment } from '@/api/departments'
import { sysUserPost } from '@/api/employees'
export default {
  props: {
    value: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      showTree: false,
      treeData: [], // 组织架构数据
      isShow: false,
      employeesData,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		姓名
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请正确输入手机号',
            trigger: 'change'
          }
        ], //	string	非必须		手机号
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		入职时间
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	number	非必须		聘用形式
        workNumber: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		工号
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	string	非必须		组织名称
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须		转正时间
      }
    }
  },
  methods: {
    closeEvent() {
      this.$emit('input', false)
      // 清空表单验证与数据
      this.$refs.form.resetFields()
      this.form = {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      }
    },
    // 确定点击
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('添加员工成功')
          this.closeEvent()
          // 刷新父级列表数据
          this.$emit('getData')
          console.log(result)
        }
      })
    },
    // 部门输入框获取焦点
    async focusEvent() {
      this.showTree = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts)
        console.log('部门', res)
      }
    },
    // 树形数据转换
    changeData(arr, pid = '') {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // el-tree点击事件
    nodeClick(data) {
      this.form.departmentName = data.name
      this.showTree = false
    },
    outFn() {
      this.showTree = false
    }
  }
}
</script>
<style lang="scss" scoped></style>
